package com.jumi.microservice.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jumi.microservice.domain.GoodsSkuDO;
import com.jumi.microservice.domain.GoodsVirtualDTO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 商品表 Mapper 接口
 * </p>
 *
 * @author liuz
 * @since 2020-07-27
 */
public interface GoodsSkuMapper extends BaseMapper<GoodsSkuDO> {

    /**
     * 统计spu虚拟销量
     *
     * @param spuId 商品spuId
     * @return spu虚拟销量
     */
    @Select("select sum(virtual_num) as total from commodity_goods_sku where goods_id = #{spuId} and is_del = 0")
    Integer countVirtualNumBySpuId(@Param("spuId") Long spuId);

    /**
     * 批量获取spu的虚拟销量
     * @param spuIds
     * @return
     */
    @Select("<script>"+
            "select goods_id spuId, sum(virtual_num) virtualNum from commodity_goods_sku where " +
            " goods_id in "+
            "<foreach collection='spuIds' item='id' open='(' separator=',' close=')'>"+
                 "#{id}"+
            "</foreach>"+
            " group by goods_id"+
            "</script>"
    )
    List<GoodsVirtualDTO> selectGoodsVirtualNum(@Param("spuIds")List<Long> spuIds);
}
